package com.yzy.fly.restAdvice;

import com.yzy.fly.api.function.response.entity.FlyApiResponseEntity;
import com.yzy.fly.api.function.response.entity.FlyBasicApiResponseEntity;
import com.yzy.fly.domain.RestMessage;
import com.yzy.fly.exception.ApiSecurityException;
import com.yzy.fly.exception.ApiAuthorityException;
import com.yzy.fly.exception.FlyBasicException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//@RestControllerAdvice(basePackages = "com.yzy.fly.**")
//@Order(999999)
@RestControllerAdvice
public class ControllerExceptionHandler {
    /**
     * 校验的统一异常处理
     * @param e
     * @return ComResponse
     *
     * BindException 有针对性的,是校验不通过时,报的错误异常类做处理
     * Exception 对所有的报错异常做处理
     */
    @ExceptionHandler(ApiAuthorityException.class)
    public RestMessage validExceptionHandler(ApiAuthorityException e) {
        // 获取我们定义的校验失败的异常信息
        RestMessage fail = RestMessage.fail(e.getMsg(), null, 0);
        fail.setCode(String.valueOf(e.getCode()));
        return fail;
    }

    @ExceptionHandler(ApiSecurityException.class)
    public RestMessage validExceptionHandler(ApiSecurityException e) {
        // 获取我们定义的校验失败的异常信息
        RestMessage fail = RestMessage.fail(e.getMsg(), null, 0);
        fail.setCode(String.valueOf(e.getCode()));
        return fail;
    }

    @ExceptionHandler(FlyBasicException.class)
    public FlyApiResponseEntity validExceptionHandler(FlyBasicException e) {
        // 获取我们定义的校验失败的异常信息
        FlyApiResponseEntity fail = FlyBasicApiResponseEntity.custom(String.valueOf(e.getCode()),e.getMsg(),null,0L);
        return fail;
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public RestMessage validExceptionHandler(IllegalArgumentException e) {
        RestMessage fail = RestMessage.fail(e.getMessage(), null, 0);
        fail.setCode("504");
        return fail;
    }

    @ExceptionHandler(RuntimeException.class)
    public RestMessage validExceptionHandler(RuntimeException e) {
        RestMessage fail = RestMessage.fail(e.getMessage(), null, 0);
        fail.setCode("500");
        return fail;
    }

}
